﻿using Micro.Extensions.Utilities;
using Micro.Srv.LogCenter.Services.Interfaces;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Micro.Srv.LogCenter.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class CommonController : ControllerBase
    {
        private readonly ISystemConfigService _systemConfig;

        public CommonController(ISystemConfigService systemConfig)
        {
            _systemConfig = systemConfig;
        }

        /// <summary>
        /// 获取日志级别
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetLogLevels")]
        public ComResult<IEnumerable<EnumDescription>> GetLogLevels()
        {
            return ComResult.Success(Enum.GetValues(typeof(LogLevel)).Cast<LogLevel>().Select(s =>
           {
               string logLevelName;
               switch (s)
               {
                   case LogLevel.Trace:
                       logLevelName = "跟踪";
                       break;
                   case LogLevel.Debug:
                       logLevelName = "调试";
                       break;
                   case LogLevel.Information:
                       logLevelName = "信息";
                       break;
                   case LogLevel.Warning:
                       logLevelName = "警告";
                       break;
                   case LogLevel.Error:
                       logLevelName = "错误";
                       break;
                   case LogLevel.Critical:
                       logLevelName = "崩溃";
                       break;
                   default:
                       logLevelName = "未知";
                       break;
               }

               return new EnumDescription(s.ToInt().ToString(), logLevelName, s.ToDescription());
           }));
        }

        /// <summary>
        /// 获取所有的appids
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetApplicationIds")]
        public async Task<ComResult<IEnumerable<EnumDescription>>> GetApplicationIds()
        {
            var response = await _systemConfig.GetSystemConfigsAsync(0, 100);
            return ComResult.Success(response.ReturnObj.Select(s => new EnumDescription(s.Id, s.Name, s.Name)));
        }

        /// <summary>
        /// 获取所有的logtype
        /// </summary>
        [HttpGet("GetLogTypes")]
        public ComResult<IEnumerable<EnumDescription>> GetLogTypes()
        {
            var logTypes = new List<EnumDescription>
            {
                new EnumDescription("Assembly", "Assembly", "Assembly"),
                new EnumDescription("Redis", "Redis", "Redis"),
                new EnumDescription("Spider", "Spider", "Spider"),
                new EnumDescription("Trace", "Trace", "Trace"),
                new EnumDescription("Mongo", "Mongo", "Mongo"),
                new EnumDescription("Restful", "Restful", "Restful"),
                new EnumDescription("DAL", "DAL", "DAL"),
                new EnumDescription("Login", "Login", "Login")
            };

            return ComResult.Success<IEnumerable<EnumDescription>>(logTypes);
        }

        [HttpGet("Init")]
        public void Init()
        {
            var bytes = System.IO.File.ReadAllBytes("appids.json");
            var jsonString = Encoding.UTF8.GetString(bytes);
            var objs = jsonString.FromJsonToObj<Dictionary<string, string>>();

            objs.Keys.ToList().ForEach(f =>
            {
                _systemConfig.AddSystemConfigAsync(new SystemConfigViewModel
                {
                    Id = f,
                    AppId = f,
                    Name = objs[f]
                }).ConfigureAwait(false).GetAwaiter().GetResult();
            });
        }
    }

    public class EnumDescription
    {
        public EnumDescription(string key, string value, string description)
        {
            Key = key;
            Value = value;
            Description = description;
        }

        public string Key { get; set; }


        public string Value { get; set; }

        public string Description { get; set; }
    }
}